<template>
  <!-- 患者列表 -->
  <section>
    <table border="0" width="100%" style="border-spacing: 0;border-collapse: collapse;">
      <tr>
        <td>
          <el-form :model="form" :inline="true" ref="form">
            <el-form-item prop="nameOrMobile">
              <el-input v-model="form.nameOrMobile" placeholder="患者姓名/手机号后4位" clearable></el-input>
            </el-form-item>
            <el-form-item prop="archivesOrMedical">
              <el-input v-model="form.archivesOrMedical" placeholder="输入档案号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="birthday">
              <!-- birthday -->
              <el-date-picker v-model="form.birthday" type="date" :editable="true" value-format="yyyy-MM-dd" placeholder="患者生日"></el-date-picker>
            </el-form-item>
            <!-- <el-form-item>
                  <el-input v-model="form.name" placeholder="会员类型"></el-input>
            </el-form-item>-->
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" @click="getData(-1)"></el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-if="qxBtns.add" plain @click="openEdit(null)">+添加</el-button>
            </el-form-item>
          </el-form>
        </td>
        <td style="text-align:right;vertical-align: top;">
          <el-button type="primary" icon="el-icon-refresh" @click="onRefresh">刷新</el-button>
          <el-button @click="exportExcel()" :disabled="!qxBtns.export" plain>导出</el-button>
        </td>
      </tr>
    </table>
    <table class="table-box">
      <thead>
        <tr>
          <th>姓名</th>
          <th>档案号</th>
          <th>手机号</th>
          <th>年龄</th>
          <th>性别</th>
          <th>来源</th>
          <th style="width:400px;">标签</th>
          <!-- <th>登记时间</th>
          <th>就诊类型</th>
          <th>诊断</th> -->
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>
            <el-button type="text" @click="getDetail(item)">{{item.name}}</el-button>
          </td>
          <td>{{item.archivesCode}}</td>
          <td>{{item.mobil}}</td>
          <td>
            <span v-if="item.age>18">{{item.age}}岁</span>
            <span v-else>{{item.age}}岁{{item.sdfMonth?item.sdfMonth:0}}月{{item.sdfDay?item.sdfDay:0}}天</span>
            <!-- <span v-else>{{item.birthday|toTimeContrast}}</span> -->
          </td>
          <td>{{item.sex | toSex}}</td>
          <td></td>
          <td style="width:400px;">
            <template v-for="(it,index) in item.labelNameList">
              <el-tag v-if="item.labelNameList.length>0" style="margin-right:3px;white-space:inherit;height:initial;" :key="index">{{it}}</el-tag>
            </template>
          </td>
          <!-- <td></td>
          <td></td>
          <td></td> -->
          <td>
            <!-- <el-link :underline="false" v-if="qxBtns.update" @click="onJieZhen(item)" type="primary">方便门诊</el-link>&nbsp; -->
            <!-- <el-link v-if="qxBtns.join" type="primary" @click="onJoin(item)">关联</el-link>&nbsp; -->
            <el-link :underline="false" v-if="qxBtns.order" @click="getOrder(item)" type="danger">预约</el-link>
            <el-link v-if="qxBtns.more" :underline="false" style="margin-left:10px;">
              <el-dropdown @command="handleCommand($event,item,index)">
                <span>
                  <span class="vertical-middle">更多</span>
                  <i class="el-icon-arrow-down el-icon--right vertical-middle"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="setvip">设置会员</el-dropdown-item>
                  <el-dropdown-item v-if="qxBtns.sign" command="sign">签名</el-dropdown-item>
                  <el-dropdown-item command="record">签名记录</el-dropdown-item>
                  <el-dropdown-item command="card">关联健康卡</el-dropdown-item>
                  <el-dropdown-item command="pay">充值</el-dropdown-item>
                  <el-dropdown-item command="off">核销记录</el-dropdown-item>
                  <!-- <el-dropdown-item command="refund">退款</el-dropdown-item> -->
                  <el-dropdown-item v-if="qxBtns.delete" command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-link>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <set v-model="dialogEdit" v-if="dialogEdit" :curItem="curItem" @success="getData"></set>
    <pay v-model="dialogJoin" v-if="dialogJoin" :curItem="curItem" @success="getData"></pay>
    <refund v-model="dialogRefund" v-if="dialogRefund" :curItem="curItem" @success="getData"></refund>
    <record v-model="dialogRecord" v-if="dialogRecord" :curItem="curItem" @success="getData"></record>
    <card v-model="showCard" :curItem="curItem" @success="getData"></card>
  </section>
</template>
<script>
import Excel from "@/_js/excel.js";
export default {
  name: "patientList",
  data() {
    return {
      dialogJoin: false,
      dialogEdit: false, //支付
      dialogRefund: false, //退款
      dialogRecord: false, //签名记录
      showCard:false,//健康卡
      curItem: null,
      pager: { pageSize: 10, pageNum: 1, total: 0 },

      tableList: [],
      form: {
        hpId: "",
        nameOrMobile: "",
        type: 2,
        archivesOrMedical: "",
        birthday: ""
      },
      treeList: [],
      qxBtns: {},
      defaultProps: {
        children: "child",
        label: "drugTypeName"
      },
      dataList: [],
      distype: "2",
      setType: "1",
      labelList: []
    };
  },
  created() {
    let th = this;
    if (th.$route.query.pageNum) {
      th.pager.pageNum = parseInt(th.$route.query.pageNum);
    }
    th.form.hpId = th.adminUser.hpId;
    th.getQX();
    th.getData();
    // th.selectLabelEmunList();
    th.addPageCache("patientList");
  },
  mounted() {
    let xw = {
      name: "小王",
      age: 24,
      say: function() {
        console.log(this.name + ":" + this.age);
      }
    };
    let xh = {
      name: "唐美莉",
      age: 23
    };
    let arr = [];
    // xw.say.call(xh);
    // xw.say.apply(xh);
  },
  methods: {
    onJoin(obj) {
      this.dialogJoin = true;
      this.curItem = obj;
    },
    onDel(obj, index) {
      let th = this;
      th.$confirm(`确定删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      }).then(() => {
        let j = { patientId: obj.patientId };
        th.showLoading();
        th.$api.deletePatient(j).then(res => {
          if (res.executed) {
            th.$message.success("删除成功");
            th.getData();
          } else {
            th.$message.error(res.message);
          }
          th.showLoading(false);
        });
      });
    },
    handleCommand(command, item, index) {
      let th = this;
      if (command == "del") {
        th.onDel(item);
      } else if (command == "setvip") {
        th.openSet(item);
      } else if (command == "pay") {
        th.onJoin(item);
      } else if (command == "refund") {
        th.openRefund(item);
      } else if (command == "record") {
        th.openRecord(item);
      } else if (command == "sign") {
        th.$router.push({
          path: "userSign",
          query: th.getUrlToken({
            id: th.jiaMi(item.patientId)
          })
        });
      } else if (command == "off") {
        th.addNavMenu(
          "套餐核销记录",
          "/page/groupObjDelRecord",
          "groupObjDelRecord"
        );
        th.$router.push({
          path: "/page/groupObjDelRecord",
          query: th.getUrlToken({
            patientId: th.jiaMi(item.patientId)
          })
        });
      }else if(command == 'card'){
         th.showCard=true;
         th.curItem=item;
      }
    },
    //健康卡
    onCard(){
      const th=this;
      th.showCard=true;
    },
    openRecord(item) {
      let th = this;
      th.dialogRecord = true;
      th.curItem = item;
    },
    openRefund(item) {
      let th = this;
      th.dialogRefund = true;
      th.curItem = item;
    },
    //刷新页面
    onRefresh() {
      const th = this;
      th.$refs["form"].resetFields();
      th.$nextTick(function() {
        th.getData();
      });
    },
    getData(type, id) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      if (id != undefined) {
        if (id == -1) {
          th.form.drugTypeId = "";
        } else {
          th.form.drugTypeId = id;
        }
      }
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      th.showLoading();
      th.$api.getThePatientLibraryList(j).then(res => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            let List = res.dataBody.list;
            let Arrary = [];
            for (let i = 0; i < List.length; i++) {
              let arr = {};
              arr = Object.assign(arr, List[i]);
              if (arr.labelIds != "" && arr.labelIds != undefined) {
                arr.labelIds = arr.labelIds.split(",");
              }
              Arrary.push(arr);
            }
            th.tableList = Arrary;
            th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        }
        th.showLoading(false);
      });
    },
    getDetail(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.patientId),
          pageNum: th.pager.pageNum
        };
      } else {
        j = {
          id: th.jiaMi(0),
          pageNum: th.pager.pageNum
        };
      }
      let js = th.$utils.JsonToUrl(th.getUrlToken(j));
      th.addNavMenu(obj.name + "档案信息", "/page/userDetail?" + js, "userDetail");
      th.$router.push({
        path: "/page/userDetail",
        query: th.getUrlToken(j)
      });
    },
    openEdit(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.id)
        };
      } else {
        j = {
          id: th.jiaMi(0)
        };
      }
      th.addNavMenu("新增患者", "/page/addPatient?"+th.$utils.JsonToUrl(th.getUrlToken(j)),'');
      th.$router.push({
        path: "/page/addPatient",
        query: th.getUrlToken(j)
      });
    },
    //预约
    getOrder(obj) {
      let th = this;
      let j = {};
      j = {
        patientId: th.jiaMi(obj.patientId),
        name: obj.name
      };

      let url = "/page/reservecentre";
      let bool = th.verifyUrl(url);
      if (bool == false) {
        th.$message.error("权限不足");
        return false;
      } else {
        th.delCache("reservecentre");
        th.addNavMenu("预约", url, "reservecentre");
        th.$router.push({
          path: url,
          query: th.getUrlToken(j)
        });
      }
    },
    openSet(curItem) {
      let th = this;
      th.dialogEdit = true;
      th.curItem = curItem;
    },
    //修改药品状态
    updateMedicalDrugStatus(obj) {
      let th = this;
      th.$api
        .updateMedicalDrugStatus({ medicalDrugId: obj.id, status: obj.status })
        .then(res => {
          if (res.executed) {
            th.$message.success("修改成功");
            th.getData();
          }
        });
    },
    exportExcel() {
      let th = this;
      th.$api
        .getThePatientLibraryList({
          hpId: th.adminUser.hpId,
          pageSize: "60000",
          pageNum: "1"
        })
        .then(res => {
          if (res.executed) {
            if (res.dataBody != undefined) {
              th.dataList = res.dataBody.list;
              let blist=th.labelList;
              let label={};
              blist.forEach((it)=>{
                label[it.labelId]=it.labelName;
              })

              Excel(
                res.dataBody.list,
                [
                  { key: "name", name: "姓名" },
                  { key: "archivesCode", name: "病历号" },
                  { key: "mobil", name: "手机号" },
                  { key: "age", name: "年龄" },
                  {
                    key: "sex",
                    name: "性别",
                    enum: { 1: "男", 2: "女", 0: "不详" },
                    type: "enum"
                  }
                  // { key: "label", name: "备注",enum:label,type:'enum' }
                ],
                "患者列表"
              );
            }
          }
          th.showLoading(false);
        });
    },
    // selectLabelEmunList() {
    //   let th = this;
    //   th.$api.selectLabelEmunList({
    //       hpId: th.adminUser.hpId,
    //       pageSize:1000,
    //       pageNum: 1
    //     })
    //     .then(res => {
    //       if (res.executed) {
    //         th.labelList = res.dataBody.list;
    //       }
    //     });
    // },
    //方便门诊
    onJieZhen(obj) {
      let th = this;
      th.$api.getRegisterListInfo({ patientId: obj.patientId }).then(res => {
        if (res.executed) {
          if (res.dataBody.list.length > 0) {
            let list = res.dataBody.list;
            let th = this;
            let j = {
              patientId: list[0].patientInfo.patientId,
              doctorId: list[0].doctorId,
              deptId: list[0].deptId,
              registerId: list[0].id,
              isFirstVisit: list[0].registerType,
              id: list[0].cureId ? list[0].cureId : null
            };
            th.showLoading();
            th.$api.doctorJieZhen(j).then(res => {
              th.showLoading(false);
              if (res.executed) {
                let url = "/page/jieZhen";
                th.$router.push({
                  path: url,
                  query: th.getUrlToken({
                    patientId: th.jiaMi(list[0].patientInfo.patientId),
                    cureId: th.jiaMi(
                      list[0].cureId ? list[0].cureId : res.data
                    ),
                    deptId: th.jiaMi(list[0].deptId),
                    doctorId: th.jiaMi(list[0].doctorId)
                  })
                });
              }
            });
          } else {
            th.$message.error("暂时没有预约信息");
          }
        } else {
          th.$message.error("暂时没有预约信息");
        }
      });
    }
  },
  components: {
    set: () => import("./_com/setVip.vue"),
    pay: () => import("./_com/payV2.vue"),
    refund: () => import("./_com/refundV2.vue"),
    record: () => import("./_com/record.vue"),
    card:()=>import("./_com/bindingCard")
  }
};
</script>
